package com.itheima.controller;

import com.itheima.commons.enums.ResultEnum;
import com.itheima.commons.enums.TipsEnum;
import com.itheima.commons.pojo.CommonEntity;
import com.itheima.commons.result.ResponseData;
import com.itheima.service.ElasticsearchDocumentService;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.rest.RestStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Class: ElasticsearchDocController
 * @Package com.itheima.controller
 * @Description:终搜平台检索控制器
 * @Company: http://www.itheima.com/
 */
@RestController
@RequestMapping("v1/docs")
public class ElasticsearchDocController {
    private static final Logger logger = LoggerFactory.getLogger(ElasticsearchDocController.class);
    @Autowired
    ElasticsearchDocumentService elasticsearchDocumentService;

    /*
     * @Description: 全文检索
     * @Method: matchQuery
     * @Param: [commonEntity]
     * @Update:
     * @since: 1.0.0
     * @Return: com.itheima.commons.result.ResponseData
     *
     */
    @GetMapping(value = "mquery")
    public ResponseData matchQuery(@RequestBody CommonEntity commonEntity) {
        // 构造返回数据
        ResponseData rData = new ResponseData();
        //定义批量查询的结果
        SearchResponse result = null;
        try {
            //调用全文检索方法
            result = elasticsearchDocumentService.matchQuery(commonEntity);
            //获取数据个数
            long aSize = result.getHits().getTotalHits().value;
            //通过类型推断自动装箱
            rData.setResultEnum(result.getHits().getHits(), ResultEnum.success, Integer.valueOf(String.valueOf(aSize)));
            //日志记录
            logger.info(TipsEnum.batch_get_doc_success.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(TipsEnum.batch_get_doc_fail.getMessage());
            rData.setResultEnum(ResultEnum.error);
        }
        //返回
        return rData;
    }

    /*
     * @Description: 结构化查询
     * @Method: matchQuery
     * @Param: [commonEntity]
     * @Update:
     * @since: 1.0.0
     * @Return: com.itheima.commons.result.ResponseData
     *
     */
    @GetMapping(value = "tquery")
    public ResponseData termQuery(@RequestBody CommonEntity commonEntity) {
        // 构造返回数据
        ResponseData rData = new ResponseData();
        //定义批量查询的结果
        SearchResponse result = null;
        try {
            //调用结构化方法
            result = elasticsearchDocumentService.termQuery(commonEntity);
            //获取数据个数
            long aSize = result.getHits().getTotalHits().value;
            //通过类型推断自动装箱
            rData.setResultEnum(result.getHits().getHits(), ResultEnum.success, Integer.valueOf(String.valueOf(aSize)));
            //日志记录
            logger.info(TipsEnum.batch_get_doc_success.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(TipsEnum.batch_get_doc_fail.getMessage());
            rData.setResultEnum(ResultEnum.error);
        }
        //返回
        return rData;
    }


    @PostMapping(value = "batch")
    public ResponseData bulkAndDoc(@RequestBody CommonEntity commonEntity) {
        // 构造返回数据
        ResponseData rData = new ResponseData();
        if (StringUtils.isEmpty(commonEntity.getIndexName()) || CollectionUtils.isEmpty(commonEntity.getList())) {
            rData.setResultEnum(ResultEnum.param_isnull);
            return rData;
        }
        //返回一个新增的状态
        RestStatus result = null;
        try {
            //调用结构化方法
            result = elasticsearchDocumentService.bulkAndDoc(commonEntity);

            //通过类型推断自动装箱
            rData.setResultEnum(result, ResultEnum.success, null);
            //日志记录
            logger.info(TipsEnum.batch_create_doc_success.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(TipsEnum.batch_create_doc_fail.getMessage());
            rData.setResultEnum(ResultEnum.error);
        }
        //返回
        return rData;
    }


    /*
     * @Description: 自动补全
     * @Method: cSuggest
     * @Param: [commonEntity]
     * @Update:
     * @since: 1.0.0
     * @Return: com.itheima.commons.result.ResponseData
     *
     */
    @GetMapping(value = "csuggest")
    public ResponseData cSuggest(@RequestBody CommonEntity commonEntity) {
        // 构造返回数据
        ResponseData rData = new ResponseData();
        if (StringUtils.isEmpty(commonEntity.getIndexName()) || StringUtils.isEmpty(commonEntity.getSuggestFileld()) || StringUtils.isEmpty(commonEntity.getSuggestValue())) {
            rData.setResultEnum(ResultEnum.param_isnull);
            return rData;
        }
        //定义批量查询返回结构
        List<String> result = null;
        try {
            //调用结构化方法
            result = elasticsearchDocumentService.cSuggest(commonEntity);

            //通过类型推断自动装箱
            rData.setResultEnum(result, ResultEnum.success, result.size());
            //日志记录
            logger.info(TipsEnum.csuggest_get_doc_success.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(TipsEnum.csuggest_get_doc_fail.getMessage());
            rData.setResultEnum(ResultEnum.error);
        }
        //返回
        return rData;
    }

    /*
     * @Description: 拼写纠错
     * @Method: pSuggest
     * @Param: [commonEntity]
     * @Update:
     * @since: 1.0.0
     * @Return: com.itheima.commons.result.ResponseData
     *
     */
    @GetMapping(value = "psuggest")
    public ResponseData pSuggest(@RequestBody CommonEntity commonEntity) {
        // 构造返回数据
        ResponseData rData = new ResponseData();
        if (StringUtils.isEmpty(commonEntity.getIndexName()) || StringUtils.isEmpty(commonEntity.getSuggestFileld()) || StringUtils.isEmpty(commonEntity.getSuggestValue())) {
            rData.setResultEnum(ResultEnum.param_isnull);
            return rData;
        }
        //定义返回纠错结果
        String result = null;
        try {
            //调用纠错方法
            result = elasticsearchDocumentService.pSuggest(commonEntity);

            //通过类型推断自动装箱
            rData.setResultEnum(result, ResultEnum.success, null);
            //日志记录
            logger.info(TipsEnum.psuggest_get_doc_success.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(TipsEnum.psuggest_get_doc_fail.getMessage());
            rData.setResultEnum(ResultEnum.error);
        }
        //返回
        return rData;
    }

    /*
     * @Description: 搜索推荐
     * @Method: tSuggest
     * @Param: [commonEntity]
     * @Update:
     * @since: 1.0.0
     * @Return: com.itheima.commons.result.ResponseData
     *
     */
    @GetMapping(value = "tsuggest")
    public ResponseData tSuggest(@RequestBody CommonEntity commonEntity) {
        // 构造返回数据
        ResponseData rData = new ResponseData();
        //判断参数
        if (StringUtils.isEmpty(commonEntity.getIndexName()) || StringUtils.isEmpty(commonEntity.getSuggestFileld()) || StringUtils.isEmpty(commonEntity.getSuggestValue())) {
            rData.setResultEnum(ResultEnum.param_isnull);
            return rData;
        }
        //定义返回推荐结果
        String result = null;
        try {
            //调用纠错方法
            result = elasticsearchDocumentService.tSuggest(commonEntity);
            //通过类型推断自动装箱
            rData.setResultEnum(result, ResultEnum.success, null);
            //日志记录
            logger.info(TipsEnum.tsuggest_get_doc_success.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(TipsEnum.tsuggest_get_doc_fail.getMessage());
            rData.setResultEnum(ResultEnum.error);
        }
        //返回
        return rData;
    }


}
